<template>
  <el-form ref="form" :model="form" :rules="rules" label-width="80px">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="form.username" maxlength="30" />
    </el-form-item> 
    <el-form-item label="部门" prop="department">
      <el-input v-model="form.department" maxlength="11" />
    </el-form-item>
    <el-form-item label="学位" prop="degree">
          <el-select v-model="form.degree" placeholder="请选择学位" maxlength="50">
            <el-option label="博士" value="博士"></el-option>
            <el-option label="硕士" value="硕士"></el-option>
            <el-option label="学士" value="学士"></el-option>
          </el-select>
        </el-form-item>
    <!-- <el-form-item label="学位" prop="degree">
      <el-input v-model="form.degree" maxlength="50" />
    </el-form-item> -->
    <!-- <el-form-item label="职称" prop="address">
      <el-input v-model="form.address" maxlength="50" />
    </el-form-item>
    <el-form-item label="性别">
      <el-radio-group v-model="form.sex">
        <el-radio label="男">男</el-radio>
        <el-radio label="女">女</el-radio>
      </el-radio-group>
    </el-form-item>-->
     <el-form-item> 
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { updateUser } from "@/api/user";
import { ElMessage } from 'element-plus';

export default {
  props: {
    user: {
      type: Object
    }
  },
  data() {
    return {
      form: {},
      // 表单校验
      rules: {
        username: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        degree: [
          { required: true, message: "学位不能为空", trigger: "blur" }
        ],
        // email: [
        //   { required: true, message: "邮箱地址不能为空", trigger: "blur" },
        //   // {
        //   //   type: "email",
        //   //   message: "请输入正确的邮箱地址",
        //   //   trigger: ["blur", "change"]
        //   // }
        // ],
        department: [
          { required: true, message: "部门不能为空", trigger: "blur" },
          // {
          //   pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
          //   message: "请输入正确的手机号码",
          //   trigger: "blur"
          // }
        ]
      }
    };
  },
  watch: {
    user: {
      handler(user) {
        if (user) {
          this.form = { username: user.username, department: user.department, degree: user.degree };
        }
      },
      immediate: true
    }
  },
  methods: {
    submit() {
  this.$refs["form"].validate(valid => {
    if (valid) {
      const updatedUser = {
        userId: this.user.userId,
        username: this.form.username,
        department: this.form.department,
        degree: this.form.degree,
      };

      updateUser(updatedUser).then(response => {
        // console.log('API Response:', response);
        if (response.status === 200) {
          ElMessage.success('修改成功');
          Object.assign(this.user, updatedUser); // 使用 Object.assign 更新 user 对象
        } else {
          ElMessage.error('服务器返回非预期响应，请检查网络连接或稍后再试');
        }
      }).catch(error => {
        console.error("更新用户信息失败:", error);
        ElMessage.error('修改失败，请稍后再试');
      });
    }
  });
},
  }
};
</script>
